﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ExcelCompare
{
    public partial class Form1 : Form
    {
        public List<SourceItem> _sourceItems;

        ExcelUtil _sourceUtil;
        string _sourceSheet;
        ExcelUtil _targetUtil;
        string _targetSheet;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (_sourceUtil != null)
            {
                _sourceUtil.Close();
            }

            if (_targetUtil != null)
            {
                _targetUtil.Close();
            }

            base.OnClosing(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofDlg = new OpenFileDialog();
            if (ofDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _sourceUtil = new ExcelUtil(ofDlg.FileName);

                try
                {
                    SelectSheetDialog sheetDlg = new SelectSheetDialog(_sourceUtil.GetSheets());
                    if (sheetDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        _sourceSheet = sheetDlg.SelectedSheet;
                        button1.Text = ofDlg.FileName;
                    }
                }
                catch (Exception ex)
                {
                    _sourceUtil = null;
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofDlg = new OpenFileDialog();
            if (ofDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _targetUtil = new ExcelUtil(ofDlg.FileName);

                try
                {
                    SelectSheetDialog sheetDlg = new SelectSheetDialog(_targetUtil.GetSheets());
                    if (sheetDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        _targetSheet = sheetDlg.SelectedSheet;
                        button2.Text = ofDlg.FileName;
                    }
                }
                catch (Exception ex)
                {
                    _targetUtil = null;
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (_targetUtil != null && _sourceUtil != null)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;

                Thread thread = new Thread(LoadSourceItems);
                thread.Start();
                //this.Invoke(new Action(LoadSourceItems));
            }
        }

        private void LoadSourceItems()
        {
            _sourceItems = _sourceUtil.GetData2(_sourceSheet, OnProcessHandle);
            if(_sourceItems==null || _sourceItems.Count==0)
            {
                OnProcessHandle(-1, -1, "读取源数据失败!");
            }
            else
            {
                _targetUtil.CompareSourceItem(_targetSheet, _sourceItems, OnProcessHandle);

                string path = _sourceUtil.SaveAs(_sourceSheet, _sourceItems, OnProcessHandle);
               if (string.IsNullOrEmpty(path))
               {
                   OnProcessHandle(-1, -1, "保存文件失败!");
               }
               else
               {
                   OnProcessHandle(-1, -1, "以保存至:" + path);
               }
            }
        }

        private void OnProcessHandle(int max, int current, string message)
        {
            this.Invoke(new Action<int, int, string>(OnProcessHandle2), max, current, message);
        }

        private void OnProcessHandle2(int max, int current, string message)
        {
            if (current == -1 && max == -1)
            {
                progressBar1.Maximum = 1;
                progressBar1.Value = 1;
                label1.Text = "结束";
                label3.Text = string.Format("{0}", message);

                button1.Enabled = true;
                button2.Enabled = true;
                button3.Enabled = true;
            }
            else
            {
                progressBar1.Maximum = max;
                progressBar1.Value = current;
                label1.Text = string.Format("{0}/{1}", current, max);
                label3.Text = message;
            }
        }
    }
}
